 var net = require('net');
const exec = require('child_process').exec

var fs = require('fs');
var path = require('path');

 var server = net.createServer();

 var sockets = [];

 server.on('connection', function(socket) {

     console.log('got a new connection');

     sockets.push(socket);

     console.log('sockets length:', sockets.length);

     var jsonData = "";
     var beginCount = 0;
     var closeCount = 0;

     var handleJson = function(data)
     {
         console.log('got',data)
         var msg = JSON.parse(data);
         console.log(msg);
         if (msg.msgtype == "command")
         {
              // 处理bat 命令
              if (msg.command != ""){
              try{
                   workerProcess = exec(msg.command, {})
                  // 不受child_process默认的缓冲区大小的使用方法，没参数也要写上{}：workerProcess = exec(cmdStr, {})
                  console.log(msg.command);
                  // 打印正常的后台可执行程序输出
                  workerProcess.stdout.on('data', function (data1) {
                     console.log('stdout: ' + data1);
                     socket.write(data1);
                  });
                 
                  // 打印错误的后台可执行程序输出
                  workerProcess.stderr.on('data', function (data1) {
                     socket.write(data1);
                  });
                 
                  // 退出之后的输出
                  workerProcess.on('close', function (code) {
                     //socket.write(data);
                  })
                  }catch(e){
                      socket.write(e);
                  }
              }
         }
         if (msg.msgtype == "com")
         {
             // 如果是串口
             
         }
     }
     var handleData = function(data)
     {
         data = data.toString();
         for (var i = 0; i < data.length;i++)
         {
             jsonData += data[i];
 
 
             if (data[i] == '{')
             {
                 beginCount ++;
             }
             if (data[i] == '}')
             {
                 closeCount++;
             }

             if (beginCount != 0 && beginCount == closeCount)
             {
                 handleJson(jsonData);
                 jsonData = "";
                 beginCount = 0;
                 closeCount = 0;
             }
             
         }
     }
     socket.on('data', function(data) {
         handleData(data);
     });

     socket.on('close', function() {
         console.log('connection closed');
         var index = sockets.indexOf(socket);
         sockets.splice(index, 1);
         console.log('sockets length:', sockets.length);
         // 同步列表信息
     });
     socket.on('error',(err)=>{
         console.log(err);
         var index = sockets.indexOf(socket);
         sockets.splice(index, 1);
         console.log('sockets length:', sockets.length);
          // 同步列表信息
     });

 });

 server.on('error', function(err) {
     console.log('Server error:', err.message);
 });

 server.on('close', function() {
     console.log('Server closed');
 });

 server.listen(4001);
